package com.hongyi.web.controller;

import com.github.pagehelper.PageInfo;
import com.hongyi.result.Result;
import com.hongyi.web.dto.SysUserDto;
import com.hongyi.web.pojo.SysPost;
import com.hongyi.web.pojo.SysRole;
import com.hongyi.web.pojo.SysUser;
import com.hongyi.web.query.SysUserQueryDto;
import com.hongyi.web.service.ISysDeptService;
import com.hongyi.web.service.ISysPostService;
import com.hongyi.web.service.ISysRoleService;
import com.hongyi.web.service.ISysUserService;
import com.hongyi.web.utils.PageResult;
import com.hongyi.web.utils.StringUtils;
import com.hongyi.web.vo.SysDeptVo;
import com.hongyi.web.vo.SysPostVo;
import com.hongyi.web.vo.SysRoleVo;
import com.hongyi.web.vo.SysUserVo;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @Author hongyi
 * @Date 2025/6/16 10:30
 */
@RestController
@RequestMapping("/sysUser")
public class SysUserController {
    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysRoleService roleService;


    @Autowired
    private ISysPostService postService;

    @Autowired
    private ISysDeptService deptService;

    /**
     * 新增用户
     */

    @PostMapping("/add")
    public Result add(@RequestBody SysUserDto user)
    {
        userService.insertUser(user);
        return Result.success("添加成功");
    }

    /**
     * 修改用户
     */
    @PutMapping("/edit")
    public Result edit(@RequestBody SysUserDto user)
    {
        userService.updateUser(user);
        return Result.success("修改成功");
    }



    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping("/getInfo")
    public Result getInfo()
    {

        HashMap<String,  Object> map = new HashMap<>();
        List<SysRole> roles = roleService.selectRoleAll();
        ArrayList<SysRoleVo> sysRoleVos = new ArrayList<>();
        if (StringUtils.isNotEmpty(roles)) {
            for (SysRole role : roles) {
                SysRoleVo sysRoleVo = new SysRoleVo();
                BeanUtils.copyProperties(role,sysRoleVo);
                sysRoleVos.add(sysRoleVo);
            }
        }
        map.put("roles", sysRoleVos);

        List<SysPost> posts = postService.selectPostAll();
        ArrayList<SysPostVo> sysPostVos = new ArrayList<>();

        if (StringUtils.isNotEmpty(posts)) {
            for (SysPost post : posts) {
                SysPostVo sysPostVo = new SysPostVo();
                BeanUtils.copyProperties(post,sysPostVo);
                sysPostVos.add(sysPostVo);
            }

        }
        map.put("posts", sysPostVos);
        return Result.success(map);
    }

    @GetMapping(value = {  "/{userId}" })
    public Result getUserById(@PathVariable("userId") Long userId)
    {


        // 根据用户userId 岗位信息
        // 查角色信息
        // 部门信息+ 基础信息

        HashMap<String,  Object> map = new HashMap<>();
//        List<SysPostVo> sysPostVos = postService.selectPostListByUserId(userId);
        SysUser sysUser = userService.selectUserById(userId);
        SysUserVo sysUserVo = new SysUserVo();
        if (sysUser!=null) {
            BeanUtils.copyProperties(sysUser, sysUserVo);
        }
        List<Long> roleIds = roleService.selectRoleListByUserId(userId);
        map.put("roleIds",roleIds);
        List<Long> postIds = postService.selectPostListByUserId(userId);
        map.put("postIds", postIds);
        map.put("data", sysUserVo);
        return Result.success(map);
    }

    /**
     * 获取部门下拉树列表
     */
    @GetMapping("/deptTree")
    public Result deptTree()
    {
        List<SysDeptVo> list = deptService.selectDeptTreeList();
        return Result.success(list);
    }

    /**
     * 获取分页
     */
    @GetMapping("/selectPage")
    public Result selectPage(SysUserQueryDto  sysUserQueryDto,
                             @RequestParam(defaultValue = "1")Integer pageNum,
                             @RequestParam(defaultValue = "10")Integer pageSize)
    {
        PageResult<SysUserVo> sysUserVoPageInfo = userService.selectPage(sysUserQueryDto, pageNum, pageSize);
        return Result.success(sysUserVoPageInfo);
    }


    @DeleteMapping("/{userIds}")
    public Result remove(@PathVariable Long[] userIds)
    {

        userService.deleteUserByIds(userIds);
        return Result.success("删除成功！");
    }

    /**
     * 状态修改
     */

    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestParam("status") String status, @RequestParam("userId") Long userId)
    {
        userService.updateUserStatus(status, userId);
        return Result.success("修改成功");
    }

}
